Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming
Passing object reference parameters
When an application passes an object reference to a method, user-defined function, or procedure, the effect is identical to assigning one object reference to another, with regard to the parameter mode (
INPUT,INPUT-OUTPUT, orOUTPUT). Thus, the assignment rules can be summarized by designating the object that provides the reference as the source and the object that receives the reference as the target. The target object reference definition can be:For more information on the rules for assigning object references, see the "Assigning object references" section.
Following are a series of examples that demonstrate the passing of object reference parameters for
INPUT,INPUT-OUTPUT, andOUTPUT. These examples highlight code from theMainclass shown previously (see the "Defining an object reference as a variable" section) and related sample classes that are described and fully listed in the "Sample classes" section. Refer to these samples for referenced class listings that are not shown in this section.When passing an
INPUTparameter, the caller is the source of the assignment and the invoked method is the target of the assignment. Therefore, the invoked method must define a parameter that is the same class as theINPUTobject reference, or a super class or interface of theINPUTobject reference. If the invoked method has as its parameter a super class or interface object reference, the invoked method can only call those methods that are defined by that super class or interface. If the invoked method attempts to call methods that are not defined in the super class or interface, the compiler generates an error.The following examples pass an
INPUTparameter:
The previous example shows an
acme.myObjs.CustObjinstance passed as input to theListDate( )method, which is defined to take anacme.myObjs.Common.CommonObj(super class ofacme.myObjs.CustObj). Note that theupdateTimestamp( )method invoked on the input object reference only exists inacme.myObjs.Common.CommonObj. (For a listing of this class, see the "Calling methods from inside a class" section.) This method can be invoked on any object that is a subclass ofacme.myObjs.Common.CommonObj. After the invocation of the method inMain, the program accesses the public data membertimestamp, which is also defined inacme.myObjs.Common.CommonObj. For more information onacme.myObjs.Common.CommonObj, see the sample classes in the "Sample classes" section.When you pass an
INPUT-OUTPUTobject then the corresponding parameter must have the same class as theINPUT-OUTPUTobject. Because the parameter is being passed in both directions, its type must match exactly.The following examples pass an
INPUT-OUTPUTparameter:
The previous example shows an
acme.myObjs.CustObjinstance passed as anINPUT-OUTPUTparameter to theListNames( )method inacme.myObjs.Common.HelperClass. In this case, the type on both sides (the passed object reference and the parameter definition) have to be exactly the same, in this caseacme.myObjs.CustObj. TheListNames( )method first uses the input object reference and stores the value in itsPRIVATEdata member,rCustObj. Then,ListNames( )instantiates a new instance ofacme.myObjs.CustObjto return as an output object reference with the same parameter, which is used back in theMainclass to invoke theCheckCredit( )method defined inacme.myObjs.CustObj.When passing an
OUTPUTparameter, the caller is the target of the assignment and the invoked method is the source of the assignment. Therefore, the caller method must define a parameter that is the same class as theOUTPUTobject reference, or a super class or interface of theOUTPUTobject reference. Once again, if the caller has as its parameter a super class or interface object reference, the caller can only invoke those methods that are defined by the super class or interface, respectively. If the caller attempts to invoke a method that is not defined in the super class, the compiler generates an error.The following examples pass an
OUTPUTparameter:
In the previous example, the
ReportOutput( )method is invoked with anOUTPUTparameter defined as an object reference to theacme.myObjs.Interfaces.IBusObjinterface. Through theOUTPUTparameter, the method inacme.myObjs.Common.HelperClassreturns an object reference toacme.myObjs.CustObj(which happens to be privately maintained by the class). This works becauseacme.myObjs.CustObjimplements theacme.myObjs.Interfaces.IBusObjinterface. The classacme.myObjs.Common.HelperClasscan return any object that implementsacme.myObjs.Interfaces.IBusObj. Note that after returning fromReportOutput( ), thelogObj( )method is invoked on the returned object reference. This method is specified in the interface and implemented inacme.myObjs.CustObj. It would be invalid to invoke a method on the returned object reference that is not specified in the interface.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |